<?php

/**
 *管理员类别
 *
*/

namespace app\admin\controller;

use app\admin\model\Adminrole as role_model;
use think\Request;

class Adminrole extends Base
{
    protected $role_model;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);

        $role_model = new role_model();
        $this->role_model = $role_model;
    }

    public function index()
    {
        return $this->listing();
    }

    public function listing()
    {
        $data = $this->role_model->all();
        $array = array();
        foreach ($data as $v) {
            $arr = array();
            $arr = $v->toArray();
            $arr['typelist'] = $this->getAdminuserList($arr['id']);
            $array[] = $arr;
        }
        $this->assign("data", $array);
        return $this->fetch('listing');
    }

//获取个分组的管理员名单
    public function getAdminuserList($id)
    {
        $result = model('adminuser')->where('role_id', $id)->column('admin_name');
        $typelist = implode(',', $result);
    
        return $typelist;
    }

//添加分组
    public function add()
    {
        if (Request::instance()->isPOST()) {
            var_dump(input('post.'));
            exit;
            if (input('post.roleName')=='') {
                $this->error("组名不能为空");
            }

            $permitdata = input('post.')['permit'];
            $data = implode(',', $permitdata);
            $this->role_model->per_ids = $data;
            $this->role_model->role_name =input('post.roleName');
            $this->role_model->note = input('post.note');
            $this->role_model->oneself = intval(input('post.oneself'));
            try {
                $this->role_model->save();
            } catch (\Exception $e) {
                $msg = $e->getMessage();
                // $msg = substr($msg,0,15);
                echo $msg;
                exit;
                $this->error("错误提示:".$msg);
            }
            $this->success('添加成功', 'listing');
        } else {
            $data = model('permission')->permitArray();
            $data = get_tree($data);
            // echo "<pre/>";
            // print_r($data);exit;
            $this->assign('data', $data);
            return $this->fetch();
        }
    }

//编辑分组
    public function edit($id = "")
    {
        $role_data = $this->role_model->get($id);

        if (Request::instance()->isPOST()) {
            // var_dump(input('post.'));
            // var_dump($id);exit;
            if (input('post.roleName')=='') {
                $this->error("组名不能为空");
            }
            $permitdata = input('post.')['permit'];
            $permitdata = implode(',', $permitdata);
            $role_data->per_ids = $permitdata;
            $role_data->role_name =input('post.roleName');
            $role_data->note = input('post.note');
            $role_data->oneself = intval(input('post.oneself'));
          
            try {
                $role_data->save();
            } catch (\Exception $e) {
                $msg = $e->getMessage();
                $msg = substr($msg, 0, 15);
// echo $msg;exit;
                $this->error("错误提示:".$msg);
            }
            $this->success('编辑成功', 'listing');
        } else {
            $role_permit_ids = $role_data->per_ids;
            $ids = explode(',', $role_permit_ids);
// var_dump($data);exit;
            $permit_data = model('permission')->permitArray();
            $permit_data = get_tree($permit_data);

            $this->assign('ids', $ids);
            $this->assign('permit_data', $permit_data);
            $this->assign('role_data', $role_data);
            return $this->fetch();
        }
    }

    public function checked($id, $ids)
    {
        if (in_array($id, $ids)) {
            return 'checked';
        } else {
            return false;
        }
    }

    public function del($id = null)
    {
        if (Request::instance()->isAjax()) {
            if ($this->role_model->destroy($id)) {
                return ['status'=>1];
            } else {
                return ['status'=>0];
            }
        }
    }
}
